<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span style="cursor:pointer">
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="addNewDepartments('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 树形组件的使用 -->
        <el-tree
          :data="list"
          :default-expand-all="true"
        >
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width:100%">
              <el-col :span="20">
                <svg-icon icon-class="naicha" /> <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data. manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addNewDepartments(data.id)">添加部门</el-dropdown-item>
                        <el-dropdown-item @click.native="getDepartmentsContent(data.id)">查看部门</el-dropdown-item>
                        <el-dropdown-item v-if="!data.children" @click.native="deleteDepartments(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
          <el-tree />
        </el-tree></el-card>
      <!-- 实现新增子部门或者编辑部门 -->
      <el-dialog
        :title=" isEdit ? '编辑' : '添加'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @closed="hClose"
      >
        <!-- 子组件 -->
        <!-- v-if="showDialog" 根据表单显示-操作创建销毁组件 -->
        <addOrEdit
          :id="curId"
          ref="depDialogRef"
          :origin-list="originList"
          :is-edit="isEdit"
          @success="hSuccess"
          @close="showDialog=false"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import addOrEdit from './deptDialog.vue'
import { getDepartments, delDepartment } from '@/api/departments'
import { tranlistToTreeList } from '@/utils/index'
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      // 保存部门的数据列表
      list: [],
      // 对话框显示与隐藏
      showDialog: false,
      // 添加部门获取到的上级部门id
      curId: '',
      // 控制打开的是添加还是查看
      isEdit: false,
      // 接收部门所有列表
      originList: []
    }
  },
  created() {
    this.loadGetDepartments()
  },
  methods: {
    // 关闭对话框
    hClose() {
      this.$refs.depDialogRef.resetForm()
    },
    // 请求全部部门数据
    async loadGetDepartments() {
      try {
        const res = await getDepartments()
        this.originList = res.data.depts.map(({ name, id, code, pid }) => ({ name, code, id, pid }))
        console.log(this.originList)
        console.log(res.data.depts)
        // 获取数据,删除第一条传智教育
        res.data.depts.shift()
        // 调用tranlistToTreeList组件将扁平数组转树形数组
        this.list = tranlistToTreeList(res.data.depts)
      } catch (err) {
        console.log(err)
      }
    },
    // 添加部门(传入点击的父级部门的id)
    addNewDepartments(id) {
      // this.$refs.depDialogRef.form = {}
      this.showDialog = true
      this.curId = id
      this.isEdit = false
    },
    // 根据id查看部门详情
    getDepartmentsContent(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = true
      // dom操作是异步的所以需要将操作放入this.$nextTick()中
      // 重新获取数据,重新渲染个人部门详情
      this.$nextTick(() => {
        // 调用子组件的方法
        this.$refs.depDialogRef.loadDetail()
      })
    },
    // 接收子组件添加部门成功事件
    hSuccess() {
      this.showDialog = false
      // 更新数据
      this.loadGetDepartments()
    },
    // 根据id删除部门
    deleteDepartments(id) {
      this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        try {
          await delDepartment(id)
          // 重新加载页面
          this.loadGetDepartments()
        } catch (err) {
          console.log(err)
        }
      }).catch(() => {
        // 取消
      })
    }
  }
}
</script>
